<?php
/******************************************************************************
 master_port.php
 Created on 18.01.2007, by Moorthy
 Purpose:
         To enter the Master port Details
/*****************************************************************************/
 include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."pagesettings/master.inc");
include(DOCINCLUDES."global_files/global_js_form.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
include(DOCINCLUDES."global_files/global_report.inc");

// include(DOCINCLUDES."global_files/global_errorpage.inc");
$action=$_GET["action"];
global $company_id;
$page->header_path="../../";

// $form=new cForm();
// $db=new cDatabase($port_id);
// $sql=new pgSql();
$report=new cReport();
$dv=new cDataValidation();
$db->setSearchPath();
if($_POST)
	{
	$dv->validations["port_name"] = array("isEmpty"=>"Enter port Name");
$columns=array("port_name");
$selected_values = getFilledRows($_POST,$columns);
if(!$selected_values)
		{
                $dv->errors["general"] = "<font color=\"red\">Enter Atleast One Record</font>";
		}
	if(!$dv->errors)
		{
		$dv->errors = validateArrayValues($selected_values);
		}
	if(!$dv->errors)
		{
		$db->beginTransaction();
		foreach($selected_values as $key => $value)
                        {
			$port_details["port_name"] = $_POST["port_name"][$key];
			if($_POST["city_id"][$key])
			{
			$port_details["city_id"] = $_POST["city_id"][$key];
			}
			if($_POST["shipment_mode_id"][$key])
			{
			$port_details["port_type_id"] = $_POST["shipment_mode_id"][$key];
			}
			$sql->table="_100351._mport";
			$sql->column=$port_details;
			$sql->generateInsertQuery();
			$db->query = $sql->query;
			$db->runQuery();
			}
		if($db->result)
			{
			$db->endTransaction();
			unset($_POST);
			$action="view_details";
			}
		}
	}
$action=($action)?$action:"view_details";
$page->title=$page->heading="Port Details";
$page->doHeaderHtml();
echo "<div align=\"center\">";
if($_GET["rtype"]!="p")
	{
	if($action=="add_details")
		{
		$report->query_string=array("action"=>"view_details");
		echo $report->returnLink($PHP_SELF,"View Details");
		}
	else
		{
		$report->query_string=array("action"=>"add_details");
		echo $report->returnLink($PHP_SELF,"Add Details");
		}
	}
if($action=="add_details")
	{
	$form->properties=array("name"=>"port_master","method"=>"POST","action"=>$PHP_SELF);
	echo $form->startForm();
	
	$db->query="select city_id,city_code,city_name from _mcity order by city_name";
	$db->runQuery();
	$city_array=$db->returnArrays();
	
	$db->query="select shipment_mode_id,shipment_mode_code,shipment_mode_name from _mship_mode order by shipment_mode_name";
	$db->runQuery();
	$shipment_mode_array=$db->returnArrays();
		for($i=0;$i<10;$i++)
			{
			$form->properties=array("type"=>"text","name"=>"port_name[$i]","value"=>"{$_POST[port_name][$i]}","size"=>"35");
			$port_detail_array[$i]["Port Name"]=$form->generateInputTag();
			$port_detail_array[$i]["Port Name"].= $dv->errors[$i]["port_name"];
			
			$form->properties=array("name"=>"city_id[$i]","class"=>"medium");
			$form->select_tag_properties=array("default"=>$_POST["city_id"][$i],"arrays"=>$city_array,"restore_default"=>"true");
			$port_detail_array[$i]["City"]=$form->generateSelectTag();
			
			$form->properties=array("name"=>"shipment_mode_id[$i]","class"=>"medium");
			$form->select_tag_properties=array("default"=>$_POST["shipment_mode_id"][$i],"arrays"=>$shipment_mode_array,"restore_default"=>"true");
			$port_detail_array[$i]["Port Type"]=$form->generateSelectTag();
			}
	$report->customization["table_style"]="width:40%; border-style:solid; border-width:thin; border-collapse:collapse;";
	$report->title="Port Entry";
	$report->highlight=true;
	$report->data = $port_detail_array;
	$report->doHorizontalTable();
	$form->properties=array("type"=>"submit","name"=>"action","value"=>"Save","accesskey"=>"3");
	echo $form->generateInputTag();
	echo $form->endForm();
	}
else
	{
	// To display the existing master port details
	$db->query="select mp.port_name as \"Port\",mct.city_name as \"City\",msm.shipment_mode_name as \"Port Type\" from _mport mp left join _mcity mct on mct.city_id=mp.city_id left join _mship_mode msm on msm.shipment_mode_id=mp.port_type_id order by port_name;";
	$db->runQuery();
	$port_detail_array=$db->returnArrays();
	$report->customization["table_style"]="width:60%; border-style:solid; border-width:thin; border-collapse:collapse;";
	$report->customization["tr_style"]="border-style:none; border-width:thin; border-collapse:collapse";
	$report->title="Port Details";
	$report->highlight=true;
	$report->data=$port_detail_array;
	$report->setNA();
	$report->doHorizontalTable();
	}
echo"</div>";
if($action=="add_details")
	$page->doFooterHtml();
else
	{
	$report->query_string=array("action"=>"view_details");
	$page->report=$report;
	$page->doFooterHtml();
	}
?>
